<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goodsData.categories">
      <!-- 面包屑 -->
      <GoodsBread :goodsData="goodsData" />
      <!-- 商品信息 -->
      <div class="goods-info">
        <!--左边-->
        <div class="media">
          <!-- 商品图片组件 -->
          <GoodsImage :images="goodsData.mainPictures" />
          <!-- 商品销售信息组件 -->
          <GoodsSales />
        </div>
        <!--右边-->
        <div class="spec">
          <!-- 商品介绍组件 -->
          <GoodsName :goodsData="goodsData" />
          <!-- sku组件  -->
          <GoodsSku
            :goodsData="goodsData"
            @change="skuChange"
            :skuId="'1369155864430120962'"
          />
          <!-- 数量选择组件 -->
          <XtxNumbox
            v-model="num"
            :max="goodsData.inventory"
            :min="1"
            label="数量"
          />
          <!-- 按钮组件 -->
          <XtxButton type="primary" size="large" @click="insertCart"
            >添加到购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品推荐 -->
      <div class="goods-relevant">
        <GoodsRelevant :goodId="goodsData.id" />
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs />
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarn />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot type="1" :goodsId="goodsData.id" />
          <GoodsHot type="2" :goodsId="goodsData.id" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findGoods } from '@/api/goods'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
import GoodsBread from './components/goods-bread'
import GoodsImage from './components/goods-image'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import GoodsRelevant from './components/goods-relevant.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsHot from './components/goods-hot'
import GoodsWarn from './components/goods-warn'
import { ref, provide } from 'vue'
import Message from '@/components/Message'
export default {
  name: 'XtxGoodsPage',
  components: { GoodsBread, GoodsImage, GoodsSales, GoodsName, GoodsRelevant, GoodsTabs, GoodsHot, GoodsWarn },
  setup () {
    const route = useRoute()
    const store = useStore()
    const goodsData = ref({})
    findGoods(route.params.id).then(data => {
      goodsData.value = data.result
    })
    const currSku = ref(null)
    // 从子组件收到的sku
    const skuChange = (sku) => {
      // console.log('父组件中，收到的sku是', sku)
      // 更新当前商品的价格
      if (sku.specsText) {
        goodsData.value.price = sku.price
        goodsData.value.oldPrice = sku.oldPrice
        goodsData.value.inventory = sku.inventory

        currSku.value = sku
      } else {
        currSku.value = null
      }
    }

    // 用户选择商品的数量
    const num = ref(1)

    // 从祖先中传递goods数据
    provide('goodsData', goodsData)

    // 收集参数（有效性判断）

    const insertCart = () => {
      if (!currSku.value) {
        Message({ type: 'error', text: '请选择商品' })
        return
      }
      if (num.value > currSku.value.inventory) {
        Message({ type: 'error', text: '数量超过了最大限制' })
        return
      }
      const cart = {
        id: goodsData.value.id, // 商品id
        skuId: currSku.value.skuId, // 有效skuId
        name: goodsData.value.name, // 商品名称
        picture: goodsData.value.mainPictures[0], // 商品图片 - 非必须
        price: currSku.value.oldPrice, // 原价格
        nowPrice: currSku.value.price, // 现价格
        count: num.value, // 商品数量
        attrsText: currSku.value.specsText, // 规格文案
        selected: true, // 是否选中
        stock: currSku.value.inventory, // 库存数量
        isEffective: true // 是否有效商品
      }

      // 调用actions做添加
      store.dispatch('cart/insertCart', cart).then(() => {
        Message({ type: 'success', text: '添加成功' })
      })
    }

    return { goodsData, skuChange, num, insertCart }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
</style>
